﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Bas;
using nERP.Model;

namespace nERP.ViewModel.Bas.CodingRuleVMs
{
    public partial class CodingRuleListVM : BasePagedListVM<CodingRule_View, CodingRuleSearcher>
    {
        
        protected override IEnumerable<IGridColumn<CodingRule_View>> InitGridHeader()
        {
            return new List<GridColumn<CodingRule_View>>{
                
                this.MakeGridHeader(x => x.CodingRule_CodingList).SetTitle(@Localizer["Page.单据编码规则主表"].Value),
                this.MakeGridHeader(x => x.CodingRule_Seq).SetTitle(@Localizer["Page.序号"].Value),
                this.MakeGridHeader(x => x.CodingRule_CodeRule).SetTitle(@Localizer["Page.规则"].Value),
                this.MakeGridHeader(x => x.CodingRule_FixedValue).SetTitle(@Localizer["Page.固定值"].Value),
                this.MakeGridHeader(x => x.CodingRule_FixedRule).SetTitle(@Localizer["Page.规则值"].Value),
                this.MakeGridHeader(x => x.CodingRule_GlideLen).SetTitle(@Localizer["Page.流水号长度"].Value),
                this.MakeGridHeader(x => x.CodingRule_StartNumber).SetTitle(@Localizer["Page.流水号起始值"].Value),
                this.MakeGridHeader(x => x.CodingRule_GlideStep).SetTitle(@Localizer["Page.递增步长"].Value),
                this.MakeGridHeader(x => x.CodingRule_Glide).SetTitle(@Localizer["Page.流水号补位码"].Value),
                this.MakeGridHeader(x => x.CodingRule_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.CodingRule_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.CodingRule_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.CodingRule_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.CodingRule_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<CodingRule_View> GetSearchQuery()
        {
            var query = DC.Set<CodingRule>()
                
                .CheckEqual(Searcher.CodingListId, x=>x.CodingListId)
                .CheckEqual(Searcher.Seq, x=>x.Seq)
                .CheckEqual(Searcher.CodeRule, x=>x.CodeRule)
                .CheckContain(Searcher.FixedValue, x=>x.FixedValue)
                .CheckEqual(Searcher.FixedRule, x=>x.FixedRule)
                .CheckEqual(Searcher.GlideLen, x=>x.GlideLen)
                .CheckEqual(Searcher.StartNumber, x=>x.StartNumber)
                .CheckEqual(Searcher.GlideStep, x=>x.GlideStep)
                .CheckContain(Searcher.Glide, x=>x.Glide)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new CodingRule_View
                {
				    ID = x.ID,
                    
                    CodingRule_CodingList = DC.Set<CodingList>().Where(z0 => z0.Code == x.CodingListId).Select(y => y.Code).FirstOrDefault(),
                    CodingRule_Seq = x.Seq,
                    CodingRule_CodeRule = x.CodeRule,
                    CodingRule_FixedValue = x.FixedValue,
                    CodingRule_FixedRule = x.FixedRule,
                    CodingRule_GlideLen = x.GlideLen,
                    CodingRule_StartNumber = x.StartNumber,
                    CodingRule_GlideStep = x.GlideStep,
                    CodingRule_Glide = x.Glide,
                    CodingRule_CreateTime = x.CreateTime,
                    CodingRule_UpdateTime = x.UpdateTime,
                    CodingRule_CreateBy = x.CreateBy,
                    CodingRule_UpdateBy = x.UpdateBy,
                    CodingRule_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class CodingRule_View: CodingRule
    {
        
        public string CodingRule_CodingList { get; set; }
        public int? CodingRule_Seq { get; set; }
        public CodeRuleEnum? CodingRule_CodeRule { get; set; }
        public string CodingRule_FixedValue { get; set; }
        public FixedEnum? CodingRule_FixedRule { get; set; }
        public int? CodingRule_GlideLen { get; set; }
        public int? CodingRule_StartNumber { get; set; }
        public int? CodingRule_GlideStep { get; set; }
        public string CodingRule_Glide { get; set; }
        public DateTime? CodingRule_CreateTime { get; set; }
        public DateTime? CodingRule_UpdateTime { get; set; }
        public string CodingRule_CreateBy { get; set; }
        public string CodingRule_UpdateBy { get; set; }
        public bool CodingRule_IsValid { get; set; }

    }

}